<template>
  <el-card shadow="never">
    <!-- 搜索 -->
    <el-form :model="searchForm" class="mb-3" size="small">
      <el-row>
        <el-col :span="6">
          <el-form-item label="關鍵詞">
            <el-input
              v-model="searchForm.keyword"
              placeholder="商品名稱"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="6">
          <el-button type="primary" @click="getData">搜索</el-button>
          <el-button @click="resetSearchForm">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 新增刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>
    <!-- <el-button type="success" size="small">总销售额:{{ totalMoney }}</el-button> -->
    <!-- 表格 -->

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="商品名" />

      <el-table-column label="分類">
        <template #default="{ row }">
          <span>{{ row.cate.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品圖">
        <template #default="{ row }">
          <el-image
            class="mr-3 rounded"
            :src="row.cover_pic"
            fit="cover"
            :lazy="true"
            style="width: 50px; height: 50px"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="商品價格" />
      <el-table-column prop="num" label="商品庫存" />
      <el-table-column prop="integral" label="積分" />
      <el-table-column prop="sale" label="銷售額" />
      <el-table-column prop="sale_num" label="銷量" />

      <el-table-column label="是否推薦">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange($event, row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px">
        <template #default="{ row }">
          <!-- <el-button
            size="small"
            text
            :type="row.banners == '' ? 'danger' : 'primary'"
            @click="handleBanner(row)"
            >设置轮播图</el-button
          > -->
          <!-- <el-button
            size="small"
            text
            :type="!row.content ? 'danger' : 'primary'"
            @click="handleSetGoodsContent(row)"
            >商品详情</el-button
          > -->

          <el-button size="small" text type="primary" @click="handleEdit(row)"
            >修改</el-button
          >

          <el-popconfirm
            title="是否要刪除?"
            confirm-button-text="確定"
            cancel-button-text="取消"
            @confirm="handleDelete(row.id)"
          >
            <template #reference>
              <el-button class="!m-0" text type="primary" size="small"
                >删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      class="flex justify-center mt-4"
      @current-change="getData"
      :current-page="curpage"
      :page-size="limit"
    />

    <!-- 提交表单 -->
    <FormDrawer
      ref="formDrawerRef"
      @submit="handleSubmit"
      :title="drawerTitle"
      size="70%"
    >
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            label-width="auto"
          >
            <el-form-item label="商品名" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="分類" prop="category_id">
              <el-select v-model="form.category_id" placeholder="選擇分類">
                <el-option
                  v-for="item in catedata"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="單位">
              <el-input v-model="form.units"></el-input>
            </el-form-item>
            <el-form-item label="現價" prop="price">
              <el-input-number v-model="form.price" :min="1" :max="10000" />
            </el-form-item>
            <el-form-item label="原價" prop="line_price">
              <el-input-number
                v-model="form.line_price"
                :min="1"
                :max="10000"
              />
            </el-form-item>
            <el-form-item label="成本" prop="cost">
              <el-input-number v-model="form.cost" :min="1" :max="10000" />
            </el-form-item>
            <el-form-item label="積分" prop="integral">
              <el-input-number v-model="form.integral" :min="1" :max="10000" />
            </el-form-item>
            <el-form-item label="庫存" prop="num">
              <el-input-number v-model="form.num" :min="1" :max="10000" />
            </el-form-item>

            <el-form-item label="封面圖">
              <ChooseImage v-model="form.cover_pic" />
            </el-form-item>

            <el-form-item label="轮播图">
              <ChooseImage v-model="form.banners" :limit="4" />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品詳情">
            <Editor v-model="form.content" />
          </el-form-item>
        </el-col>
      </el-row>
    </FormDrawer>
    <!-- 设置轮播图 -->
    <!-- <Banner ref="banner" @reload-data="getData" /> -->
    <!-- <Content ref="contentRef" @reload-data="getData" /> -->
  </el-card>
</template>

<script setup>
import { ref } from "vue";

import {
  getProduct,
  createProduct,
  updateProduct,
  deleteProduct,
} from "~/api/product.js";
import { getCategory } from "~/api/category.js";
import FormDrawer from "~/components/FormDrawer.vue";
import ListHeader from "~/components/ListHeader.vue";
import ChooseImage from "~/components/ChooseImage.vue";
import Banner from "./banner.vue";
import Content from "./content.vue";
import { useInitTable, useInitForm } from "~/composables/useCommon.js";
import Editor from "~/components/Editor.vue";
const role = ref(null);

const totalMoney = ref(0);
const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  curpage,
  total,
  limit,
  getData,
  handleDelete,
  handleStatusChange,
} = useInitTable({
  searchForm: {
    keyword: "",
  },
  getList: getProduct,
  delete: deleteProduct,
  updateStatus: updateProduct,
  onGetListSuccess: (res) => {
    totalMoney.value = res.totalMoney;
    tableData.value = res.data.data;
    total.value = res.data.total;
  },
});

//表单

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
} = useInitForm({
  form: {
    name: "",
    price: 0,
    line_price: 0,
    num: 100,
    category_id: null,
    cover_pic: "",
    content: "",
    integral: 1,
    cost: 1,
    banners: "",
    units: "",
  },
  rules: {
    name: [{ required: true, message: "商品名不能為空", trigger: "blur" }],
  },
  getData,
  update: updateProduct,
  create: createProduct,
});

const catedata = ref([]);
//分类数据
const category = () => {
  getCategory().then((res) => {
    catedata.value = res;
  });
};
category();

const banner = ref(null);
function handleBanner(e) {
  banner.value.open(e);
}

// 设置商品详情
const contentRef = ref(null);
const handleSetGoodsContent = (row) => contentRef.value.open(row);
</script>
